package com.zbcbms.controller;

import com.zbcbms.common.GetChartCommon;
import com.zbcbms.entity.tableData.BookBorrowTableData;
import com.zbcbms.entity.dto.UserMap;
import com.zbcbms.service.BookBorrowService;
import com.zbcbms.service.impl.BookBorrowServiceImpl;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.PieChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class SelfBorrowingController implements Initializable {
    @FXML
    private TableView<BookBorrowTableData> searchBookBorrowTableView;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchId;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchUsername;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchBorrowTime;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchReturnTime;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchBookName;
    @FXML
    private TableColumn<BookBorrowTableData,String> OperationName;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchStatus;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchBookNumber;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchUserNumber;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchDeadline;
    @FXML
    private LineChart<String, Long> BorrowCountLineChart;
    @FXML
    private PieChart bookStatusPieChart;
    @FXML
    private ComboBox<String> remainingDay;
    @FXML
    private TextField searchText;

    private GetChartCommon getChartCommon=new GetChartCommon();
    private BookBorrowService bookBorrowService=new BookBorrowServiceImpl();
    private ObservableList<BookBorrowTableData> data = new BookBorrowServiceImpl().getBorrowRecordByUser(UserMap.getNumber());


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initRemainingDay();
        bookBorrowTableInit(searchBookBorrowTableView,searchId,searchUsername,searchBorrowTime,searchReturnTime,
                searchBookName,OperationName,searchStatus,searchBookNumber,searchUserNumber,searchDeadline,data);
        BorrowCountLineChart.setData(getChartCommon.getLineChartData());
        bookStatusPieChart.setData(getChartCommon.getBookBorrowChartData());
    }

    public void bookBorrowTableInit(TableView<BookBorrowTableData> tableView,
                                    TableColumn<BookBorrowTableData,String> searchId,
                                    TableColumn<BookBorrowTableData,String> searchUsername,
                                    TableColumn<BookBorrowTableData,String> searchBorrowTime,
                                    TableColumn<BookBorrowTableData,String> searchReturnTime,
                                    TableColumn<BookBorrowTableData,String> searchBookName,
                                    TableColumn<BookBorrowTableData,String> OperationName,
                                    TableColumn<BookBorrowTableData,String> searchStatus,
                                    TableColumn<BookBorrowTableData,String> searchBookNumber,
                                    TableColumn<BookBorrowTableData,String> searchUserNumber,
                                    TableColumn<BookBorrowTableData,String> searchDeadline,
                                    ObservableList<BookBorrowTableData> data){
        searchId.setCellValueFactory(cellData->cellData.getValue().getId());
        searchUsername.setCellValueFactory(cellData->cellData.getValue().getUserName());
        searchBorrowTime.setCellValueFactory(cellData->cellData.getValue().getBorrowDate());
        searchReturnTime.setCellValueFactory(cellData->cellData.getValue().getReturnDate());
        searchBookName.setCellValueFactory(cellData->cellData.getValue().getBookName());
        OperationName.setCellValueFactory(cellData->cellData.getValue().getOperatorName());
        searchUserNumber.setCellValueFactory(cellData->cellData.getValue().getUserNumber());
        searchBookNumber.setCellValueFactory(cellData->cellData.getValue().getBookNumber());
        searchStatus.setCellValueFactory(cellData->cellData.getValue().getStatus());
        searchDeadline.setCellValueFactory(cellData->cellData.getValue().getDeadline());
        tableView.setItems(data);
    }

    @FXML
    public void searchBtnAction(){
        String text = searchText.getText();
        ObservableList<BookBorrowTableData> list = bookBorrowService.getBookBorrowVoByKeywordAndUserNumber(text,UserMap.getNumber());
        bookBorrowTableInit(searchBookBorrowTableView,
                searchId,searchUsername,searchBorrowTime,searchReturnTime,searchBookName,
                OperationName,searchStatus,searchBookNumber,searchUserNumber,searchDeadline,list);
    }

    public void initRemainingDay(){
        List<String> nums = Arrays.asList("3","5", "7","30","全部","已过期");
        ObservableList<String> obList = FXCollections.observableArrayList(nums);
        remainingDay.setItems(obList);
        remainingDay.getSelectionModel().select(4);
    }

    public void RemainingDayAction(){
        String days = remainingDay.getValue();
        ObservableList<BookBorrowTableData> bookBorrowByUserAndDay = bookBorrowService.getBookBorrowByUserAndDay(UserMap.getNumber(), days);
        bookBorrowTableInit(searchBookBorrowTableView,searchId,searchUsername,searchBorrowTime,searchReturnTime,
                searchBookName,OperationName,searchStatus,searchBookNumber,searchUserNumber,searchDeadline,bookBorrowByUserAndDay);
    }
}
